package com.bw.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.copier.Copier;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.SalaryDataDetail;
import com.bw.config.UploadMinioUtils;
import com.bw.domain.Section;
import com.bw.service.SectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.metric.consumer.HystrixDashboardStream;
import org.apache.poi.ss.formula.functions.T;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.dao.support.DaoSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class ListController {
    @Autowired
    SectionService sectionService;

    @RequestMapping("/list")
    public Result listSections(@RequestBody Section section) {
        List<Section> list = sectionService.list1(section);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> build = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getDeptId() + "");
                    tree.setParentId(treeNode.getParentId() + "");
                    tree.setName(treeNode.getDeptName());
                    //扩展属性
                    tree.putExtra("deptId", treeNode.getDeptId());
                    tree.putExtra("deptName", treeNode.getDeptName());
                    tree.putExtra("orderNum", treeNode.getOrderNum());
                    tree.putExtra("status", treeNode.getStatus());
                    tree.putExtra("leader", treeNode.getLeader());
                    tree.putExtra("phone", treeNode.getPhone());
                    tree.putExtra("email", treeNode.getEmail());
                    tree.putExtra("createTime", treeNode.getCreateTime());
                    tree.putExtra("files", treeNode.getFiles());
                });

        return  Result.success(build);
    }  // 获取所有节目列表
   @RequestMapping("/deptList")
    public Result deptList() {
        List<Section> list = sectionService.deptList();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> build = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getDeptId() + "");
                    tree.setParentId(treeNode.getParentId() + "");
                    tree.setName(treeNode.getDeptName());
                    //扩展属性
                    tree.putExtra("deptId", treeNode.getDeptId());
                    tree.putExtra("deptName", treeNode.getDeptName());
                    tree.putExtra("orderNum", treeNode.getOrderNum());
                    tree.putExtra("status", treeNode.getStatus());
                    tree.putExtra("createTime", treeNode.getCreateTime());
                    tree.putExtra("leader", treeNode.getLeader());
                    tree.putExtra("phone", treeNode.getPhone());
                    tree.putExtra("email", treeNode.getEmail());
                    tree.putExtra("files", treeNode.getFiles());
                });

        return  Result.success(build);
    }  // 获取所有节目列表

    @RequestMapping("add")
    public Result add(@RequestBody Section section){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        section.setCreateTime(simpleDateFormat.format(new Date()));
        boolean add= sectionService.add(section);
        return  add?Result.success():Result.fail();
    }
    @RequestMapping("uplore")
    public Result uplore(@RequestBody Section section){
        boolean uplore= sectionService.uplore(section);

        return  uplore?Result.success():Result.fail();
    }
    @RequestMapping("remove")
    public Result remove(Integer deptId){
        boolean remove= sectionService.remove(deptId);
        return  remove?Result.success():Result.fail();
    }

    @RequestMapping("excel")
    public  void excel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("部门信息", "UTF-8").replaceAll("\\+", "%20");
       //设置响应的编码
        response.setCharacterEncoding("UTF-8");
        //设置响应的类型:xlsx 后缀的文件
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //设置响应头，告诉浏览器以下载的为一个excel表格
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Section> data = sectionService.list();
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        EasyExcel.write(response.getOutputStream(), Section.class).sheet("部门信息").doWrite(data);
    }

    @RequestMapping("deptGetDept")
    public Result deptGetDept(Integer id){
        Section section = sectionService.deptGetDept(id);
        return Result.success(section);
    }
    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        String s = UploadMinioUtils.uploadPic(file);
        return s;
    }

    @RequestMapping("/excels")
    public String upload1(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Section.class, new UploadDataListener(sectionService)).sheet().doRead();
        return "success";
    }

}
